#include <iostream>
#include <vector>
#include <sstream>
#include <queue>

int direction_vec[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

void parseStr(std::string s, std::vector<int> &ns)
{
    std::istringstream iss(s);
    std::string tok;
    while (std::getline(iss, tok, ','))
    {
        ns.push_back(std::stoi(tok));
    }
}

int main()
{
    std::string sz_str;
    std::vector<int> sz_vec;
    std::getline(std::cin, sz_str);
    parseStr(sz_str, sz_vec);
    int n = sz_vec[0];
    int m = sz_vec[1];

    std::vector<std::vector<int>> mp;
    std::vector<std::vector<int>> dis;
    std::vector<std::vector<bool>> vis;
    for (int i = 0; i < n; i++)
    {
        std::string dis_str;
        std::vector<int> dis_vec;
        std::getline(std::cin, dis_str);
        parseStr(dis_str, dis_vec);

        mp.push_back(dis_vec);
        dis.push_back(std::vector<int>(m, 1e9));
        vis.push_back(std::vector<bool>(m, false));
    }

    dis[n - 1][m - 1] = mp[n - 1][m - 1];
    vis[n - 1][m - 1] = true;
    std::queue<std::pair<int, int>> q;
    q.push({n - 1, m - 1});

    while (q.size())
    {
        std::pair<int, int> p = q.front();
        q.pop();
        vis[p.first][p.second] = true;
        for (int i = 0; i < 4; i++)
        {
            int nx = p.first + direction_vec[i][0];
            int ny = p.second + direction_vec[i][1];
            if (nx < 0 || nx >= n || ny < 0 || ny >= m)
                continue;
            if (mp[nx][ny] == 0)
                continue;
            int nxtval = 0;
            if (mp[nx][ny] > 0)
                nxtval = dis[p.first][p.second] + mp[nx][ny];
            if (nxtval > 100)
                continue;
            if (dis[nx][ny] > nxtval)
            {
                dis[nx][ny] = nxtval;
                if (!vis[nx][ny])
                {
                    vis[nx][ny] = 1;
                    q.push({nx, ny});
                }
            }
        }
    }

    int ans = -1;
    if (dis[0][0] <= 100)
        ans = dis[0][0];
    std::cout << ans << std::endl;

    return 0;
}